#define _CRT_SECURE_NO_WARNINGS 1

#include <Windows.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Stack
{
    int x;
    struct Stack* next;
}Stack;
Stack* _CreateStackNode(int x)
{
    Stack* node = (Stack*)calloc(1, sizeof(Stack));
    node->x = x;
    node->next = NULL;
    return node;
}
void StackInit(Stack** st)
{
    if (st == NULL)
        return;
    *st = _CreateStackNode(0);
}
void StackDestroy(Stack** st)
{
    if (st == NULL)
        return;
    while (*st != NULL)
    {
        Stack* node = *st;
        *st = (*st)->next;
        free(node);
    }
}
_Bool StackEmpty(Stack* st)
{
    return (st->next == NULL);
}
void StackPush(Stack* st, int x)
{
    Stack* node = _CreateStackNode(x);
    node->next = st->next;
    st->next = node;
}
void StackPop(Stack* st)
{
    if (st->next == NULL)
        return;
    Stack* node = st->next;
    st->next = node->next;
    free(node);
}
int StackTop(Stack* st)
{
    if (st->next == NULL)
        return;
    return st->next->x;
}
int StackSize(Stack* st)
{
    if (st == NULL)
        return;
    int count = 0;
    st = st->next;
    while (st == NULL)
    {
        count++;
        st = st->next;
    }
    return count;
}

typedef struct
{
    Stack* pop;
    Stack* push;
}MyQueue;


MyQueue* myQueueCreate()
{
    MyQueue* mq = (MyQueue*)calloc(1, sizeof(MyQueue));
    StackInit(&mq->pop);
    StackInit(&mq->push);
    return mq;
}

void myQueuePush(MyQueue* obj, int x)
{
    StackPush(obj->push, x);
}

int myQueuePop(MyQueue* obj)
{
    if (StackEmpty(obj->pop))
    {
        while (!StackEmpty(obj->push))
        {
            StackPush(obj->pop, StackTop(obj->push));
            StackPop(obj->push);
        }
    }
    int ret = StackTop(obj->pop);
    StackPop(obj->pop);
    return ret;
}

int myQueuePeek(MyQueue* obj)
{
    if (StackEmpty(obj->pop))
    {
        while (!StackEmpty(obj->push))
        {
            StackPush(obj->pop, StackTop(obj->push));
            StackPop(obj->push);
        }
    }
    return StackTop(obj->pop);
}

bool myQueueEmpty(MyQueue* obj)
{
    return StackEmpty(obj->push) && StackEmpty(obj->pop);
}

void myQueueFree(MyQueue* obj)
{
    StackDestroy(&obj->pop);
    StackDestroy(&obj->push);
    free(obj);
}
int main()
{
    MyQueue* test = myQueueCreate();
    for (int i = 1; i <= 10; i++)
        myQueuePush(test, i);
    while (!myQueueEmpty(test))
        printf("%d ", myQueuePop(test));
    printf("\n");
    myQueueFree(test);
	return 0;
}